<template>
  <transition name="fade" @before-leave="onClose" @after-leave="onDestroy">
    <div
      :style="{
        top: topOffset + 'px',
        backgroundColor: 'white',
      }"
      class="message"
      v-if="visiable"
    >
      <img :src="require('./' + icon)" />
      {{ message }}
    </div>
  </transition>
</template>

<script>
/* eslint-disable */
export default {
  props: {
    topOffset: {
      type: Number,
      required: true,
    },
    message: {
      type: String,
      required: true,
    },
    type: {
      type: String,
      default: "success",
    },
    duration: {
      type: Number,
      default: 1500,
    },
    id: {
      type: Number,
      required: true,
    },
    onDestroy: {
      type: Function,
      required: true,
    },
    onClose: {
      type: Function,
      required: true,
    },
  },
  data() {
    return {
      icon: "勾.png",
      visiable: false,
    };
  },
  methods: {
    close() {
      this.visiable = false;
    },
  },
  mounted() {
    this.icon = this.type === "success" ? "勾.png" : "叉.png";
    this.visiable = true;
    setTimeout(() => {
      this.close();
    }, this.duration);
  },
};
</script>

<style lang="less"  scoped>
@import "../../assets/styles/variables.less";

.message {
  position: fixed;
  right: -5%;
  transform: translate(-50%, 15px);
  padding: 10px;
  width: 300px;
  z-index: 999999;
  transition: top 0.4s;
  border-radius: 5px;
  font-size: 17px;
  text-align: center;
  border: 2px solid @mainColor;
}

img {
  width: 15px;
  margin-top: -2px;
}

.fade-enter-active,
.fade-leave-active {
  transition: transform 0.4s, opacity 0.4s;
}

.fade-enter-from {
  transform: translate(-50%, 0);
  opacity: 0;
}

.fade-leave-to {
  transform: translate(-50%, -10px) !important;
  opacity: 0;
}
</style>
